<!DOCTYPE HTML>
<html>
<head>
  <title>Web SQL database</title>
  <script src="../../test_bootstrap.js"></script>
  <script src="../../../atoms/inject/deps.js"></script>
  <script type='text/javascript'>
    goog.require('bot');
    goog.require('bot.html5');
    goog.require('goog.json');
    goog.require('goog.Promise');
    goog.require('goog.testing.jsunit');
    goog.require('webdriver.atoms.inject.storage.database');
  </script>

  <script type='text/javascript'>
    /**
     * Initial function to create the database. I use the top-level window
     * to create the database.
     * This function must not fail or throw error for of the test functions.
     */
    function setUpPage() {
      if (!bot.html5.isSupported(bot.html5.API.DATABASE)) {
        return;
      }
      return new goog.Promise(function(success, fail) {
        var win = bot.getWindow();
        // Keep the DB size below 5 MB. Above that size, Safari will prompt for
        // permission and cause this test to hang.
        var db = win.openDatabase('testDB', '1.0', 'db name', 2 * 1024 * 1024);
        db.transaction(function(tx) {
          tx.executeSql('CREATE TABLE IF NOT EXISTS docids (id INTEGER \
              PRIMARY KEY, name TEXT, owner TEXT)');
          tx.executeSql(
            'INSERT OR REPLACE INTO docids VALUES (11, "aa", "Manager")');
          tx.executeSql(
            'INSERT OR REPLACE INTO docids VALUES (1, "aabb", "Eng-A")');
          tx.executeSql(
            'INSERT OR REPLACE INTO docids VALUES (31, "abc", "Eng-B")');
          tx.executeSql(
            'INSERT OR REPLACE INTO docids VALUES (0, "aabb", "Eng-A")');
          tx.executeSql(
            'INSERT OR REPLACE INTO docids VALUES (2, "yyy", "Eng-D")');
          tx.executeSql(
            'INSERT OR REPLACE INTO docids VALUES (3, "zzz", "Eng-A")');
          tx.executeSql(
            'INSERT OR REPLACE INTO docids VALUES (90, "aabb", "Eng-E")');
        }, fail, success);
      });
    }

    function testExecuteSqlWithErrorWrapsErrorInJsonResult() {
      if (!bot.html5.isSupported(bot.html5.API.DATABASE)) {
        return;
      }

      return new goog.Promise(function(onDone) {
        // WITH is erroneously used instead of WHERE
        webdriver.atoms.inject.storage.database.executeSql(
            'testDB',
            'SELECT * from docids WITH id = 1',
             [], onDone);
      }).then(function(result) {
        assertEquals(bot.ErrorCode.SQL_DATABASE_ERROR,
            goog.json.parse(result)["status"]);
      });
    }

    function testExecuteSqlReturnsSuccessInJsonResult() {
      if (!bot.html5.isSupported(bot.html5.API.DATABASE)) {
        return;
      }
      return new goog.Promise(function(onDone) {
        webdriver.atoms.inject.storage.database.executeSql(
            'testDB',
            'SELECT * from docids WHERE id = 1',
             [], onDone);
      }).then(function(result) {
        assertEquals(bot.ErrorCode.SUCCESS,
            goog.json.parse(result)["status"]);
      });
    }
  </script>
</head>
<body>
</body>
</html>
